/****************************************************************************************************************
* Copyright � Mathematica Policy Research, Inc.
* This code cannot be copied, distributed or used without the express written permission
* of Mathematica Policy Research, Inc.
   Program	   				: 100_cp_ssi_status_verification.sas
   Purpose     				: Use enrollment codes to verify states have sufficiently reliable SSI status Medicaid by
							  benchmarking to published statistics
   Programmer  				: Claire Erba
   Researcher  				: Michael Levere
   Specificatn 				: N/A
   Project Name				: DIL - SSI
   Project Number			: 055827
   Date Created				: 12/15/2022
   Runtime     				: TBD
   QA          				: Michael Levere
   Input files 				: tafr17.demog_elig_base - tafr19.demog_elig_base
							  tafr17.demog_elig_disability - tafr19.demog_elig_disability
   Output files				: _100_ssi_elig_counts

   Modifications or Updates	: 12/02/2022 - New program.
*********************************************************************************************************************/
*====================================================================*;        
* Set Up															 *;
*====================================================================*;
/* Set up log/lst/html output files */
%let datestamp = %sysfunc(date(),yymmddn8.);
%let logfile = %sysfunc(tranwrd(&_SASPROGRAMFILE., .sas, _&datestamp..log));
%let lstfile = %sysfunc(tranwrd(&_SASPROGRAMFILE., .sas, _&datestamp..lst));
%let htmlfile = %sysfunc(tranwrd(&_SASPROGRAMFILE., .sas, _&datestamp..html));
proc printto 
    log="&logfile." 
    print="&lstfile." new; 
run;
ods html body="&htmlfile." style=HTMLBlue;

/* Obtain starting time */
%let timer_start = %sysfunc(datetime());
data _null_;
	now = &timer_start.;
	put "Timer starting at " now dateampm.;
run;

/* Options */
options mprint symbolgen center;
options ls=154 ps=63 formchar="�----�+�---+=�-/\<>*" msglevel=I;
options mergenoby=warn dkrocond=warn dkricond=warn compress=no;
options varinitchk=warn dtreset source;
options validvarname=any;

/* Set paths and libraries */
%let run_dir = &myfiles_root./dua_055827/SSI Medicaid;
x "mkdir &myfiles_root./dua_055827/Users/&ouser./ssi";
libname permwork "&myfiles_root./dua_055827/Users/&ouser./ssi";
options user = permwork minoperator; 
libname out "&run_dir./ssi_status_verification/data";

*====================================================================*;        
* Main Macro														 *;
*====================================================================*;
%macro ssi_status_verification;

	/* Loop over 2017-2019 */
	%do yr = 17 %to 19;

		/* Read in demog_elig_base data */
		data demog_elig_base_&yr.;
			set tafr&yr..demog_elig_base;
			keep bene_id msis_id state_cd misg_elgblty_data_ind birth_dt death_dt elgblty_grp_cd_12;
		run;

		/* Merge on demog_elig_disability data */
		proc sort data = demog_elig_base_&yr.;
			by msis_id state_cd;
		run;
		proc sort data = tafr&yr..demog_elig_disability (keep = msis_id state_cd ssi_ind_12 ssi_stus_cd_12) out = demog_elig_disability_&yr.;
			by msis_id state_cd;
		run;
		data demog_elig_base_dsblty_&yr.;
			merge demog_elig_base_&yr. (in=a) demog_elig_disability_&yr. (in=b);
			by msis_id state_cd;
			length _merge $20.;
			if a and b then _merge = "MERGED";
			if a and not b then _merge = "ONLY IN BASE";
			if not a and b then _merge = "ONLY IN DISABILITY";
		run;

		/* Run a frequency of the merge and then limit to if in base */
		title "Frequency of Merge Between DEMOG_ELIG_BASE and DEMOG_ELIG_DISABILITY";
		title2 "There Should Not Be Any ONLY IN DISABILITY";
		title3 "20&yr.";
		proc freq data = demog_elig_base_dsblty_&yr.;
			table _merge / list missing;
		run;

		/* Limit to persons of interest (misg_elgblty_data_ind ne 1 and age in dec of year < 18) */
		/* Create flags for SSI status */
		data demog_elig_base_dsblty_&yr._2;
			set demog_elig_base_dsblty_&yr.;
			if not missing(birth_dt) then do;
				if not missing(death_dt) and death_dt < mdy(12,1,20&yr.) then age_dec_20&yr. = floor(yrdif(birth_dt, death_dt, "AGE"));
				else age_dec_20&yr. = floor(yrdif(birth_dt, mdy(12,1,20&yr.), "AGE"));	
			end;
			if misg_elgblty_data_ind ne 1 and age_dec_20&yr. >= 0 and age_dec_20&yr. < 18;
			if elgblty_grp_cd_12 in (11, 12, 13) then elig_code = 1;
			else elig_code = 0;
			if ssi_ind_12 = 1 then ssi_flag = 1;
			else ssi_flag = 0;
			if ssi_stus_cd_12 in ("001", "002") then ssi_status = 1;
			else ssi_status = 0;
			if elig_code = 1 or ssi_flag = 1 or ssi_status = 1 then any_ssi = 1;
			else any_ssi = 0;
			drop _merge;
		run;

		/* Run frequencies to ensure data is correct */
		title "Frequencies To Ensure Variables Were Created Correctly";
		title2 "20&yr.";
		proc freq data = demog_elig_base_dsblty_&yr._2;
			table 
				misg_elgblty_data_ind
				age_dec_20&yr.
				elgblty_grp_cd_12 * elig_code
				ssi_ind_12 * ssi_flag
				ssi_stus_cd_12 * ssi_status
				elig_code * ssi_flag * ssi_status * any_ssi
				/ list missing;
		run;

		/* Create counts of number of eligible children by state */
		proc means data = demog_elig_base_dsblty_&yr._2 nway missing noprint;
			class state_cd;
			output out = demog_elig_base_dsblty_&yr._3 (drop = _TYPE_ _FREQ_
														rename = elig_code = count_elig_code_20&yr. 
														rename = ssi_flag = count_ssi_flag_20&yr. 
														rename = ssi_status = count_ssi_status_20&yr.
														rename = any_ssi = count_any_ssi_20&yr.)


			sum(elig_code ssi_flag ssi_status any_ssi)=;
		run;

	%end;

	/* Merge yearly data */
	data out._100_ssi_elig_counts;
		merge demog_elig_base_dsblty_17_3 (in=a) demog_elig_base_dsblty_18_3 (in=b) demog_elig_base_dsblty_19_3 (in=c);
		by state_cd;
	run;

	/* Export to Excel file */
	proc export data = out._100_ssi_elig_counts outfile = "&run_dir./ssi_status_verification/data/_100_ssi_elig_counts.xlsx"
		dbms = xlsx
		replace;
		sheet = "SSI Elig Counts";
	run;

	/* Proc means on output data */
	title "OUTPUT DATA FINAL (STATE LEVEL): Means";
	proc means data = out._100_ssi_elig_counts n nmiss sum mean std min max;
	run;

	/* Proc contents on output data */
	title "OUTPUT DATA FINAL (STATE LEVEL): Contents";
	proc contents data = out._100_ssi_elig_counts;
	run;

%mend ssi_status_verification;
%ssi_status_verification;

*====================================================================*;        
* Clean Up															 *;
*====================================================================*;
/* Add program run time to the log */
data _null_;
	now = datetime();
	dur = datetime() - &timer_start.; *-- doing datetime() twice for most accuracy, probably overkill ;
	put "Timer ending at" now dateampm.;
    put 30*"-" / " TOTAL DURATION:" dur TIME13.2 / 30*"-";
run;

/* Delete datasets from the working library */
proc datasets lib = permwork kill nolist;
run;

proc printto;
run;

ods html close;
